package org.jetnuke.modules.blog;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.entities.Theme;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.BlogCategoryDAO;
import org.jetnuke.dao.BlogPostDAO;
import org.jetnuke.dao.BlogUserSettingsDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.BlogCategory;
import org.jetnuke.entities.BlogUserSettings;
import org.jetnuke.entities.MainConfig;
import org.jetnuke.modules.blog.utils.BlogUtils;

public class PreviewTheme implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(PreviewTheme.class);

	int theme;

	public void setTheme(int theme) {
		this.theme = theme;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		String _styleFolder = "_styleFolder";
		String currentStyle = null;
		try {
			Theme tm = net.jforum.dao.DataAccessDriver.getInstance()
					.newThemeDAO().selectByID(theme);
			currentStyle = tm.getStyleName();
		} catch (Throwable e) {
			logger
					.warn(
							"exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - exception ignored", e); //$NON-NLS-1$

		}
		if (currentStyle != null)
			vc.put(_styleFolder, "style/" + currentStyle);
		vc.put("_currentStyle", currentStyle);

		int id = ui.getID();
		net.jforum.entities.User usr = DataAccessDriver.getInst().newUserDAO()
				.selectById(id);

		BlogCategoryDAO bcm = DataAccessDriver.getInst().newBlogCategoryDAO();
		BlogPostDAO bpm = DataAccessDriver.getInst().newBlogPostDAO();
		BlogUserSettingsDAO busm = DataAccessDriver.getInst()
				.newBlogUserSettingsDAO();
		BlogUserSettings bus = busm.retrieveByPk(usr.getId());

		List categoryList = bcm.selectByUser(id);

		Iterator it = categoryList.iterator();

		List blogList = null;
		int limit = bus.getMAXNUMBERPOST();
		if (limit < 1)
			limit = 1;
		if (limit > 10)
			limit = 10;

		vc.put("my_blog", Boolean.TRUE);

		GregorianCalendar cal = null;

		cal = new GregorianCalendar();
		blogList = bpm.retrievePublishByUser(id, limit);

		vc.put("blogList", blogList);

		HashMap cp = new HashMap();
		vc.put("cp", cp);
		it = categoryList.iterator();
		while (it.hasNext()) {
			BlogCategory bc = (BlogCategory) it.next();
			cp.put(new Integer(bc.getID()), bc);
		}

		vc.put("settings", bus);
		vc.put("_pagetitle", " - " + bus.getTITLE());
		MainConfig config = (MainConfig) request.getAttribute("MAIN_CONFIG");
		vc.put("nukeUrl", config.getNUKEURL());
		List preLeftBlocks = new ArrayList();
		preLeftBlocks.add(BlogUtils.createUserBlockInfo(vc, usr));

		preLeftBlocks.add(BlogUtils.createCalendarBlock(vc, cal, id));

		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

}
